/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package nadesicoi

import java.util.Calendar;
import groovy.inspect.swingui.*

/**
 *
 * @author Usuario
 */
class TareasProgramadas {
    def intervalo = 10000
    def tareas    = [[hora:800, tarea:"Actualizar Nadro", comando:"nadesicoi.ActualizadorNadro.start()"]]
    def registros = []

    TareasProgramadas() {}
    def start() {
        Thread.start {
            def hora, h, m, tarea, registro, fecha
            def f            = { concepto-> return (int)hora.get(Calendar."$concepto") }
            def getDia       = { return Integer.valueOf(String.format("%d%02d%02d", f("YEAR"),f("MONTH"),f("DAY_OF_MONTH"))) }
            while(true) {
                for(i in 0..tareas.size()-1) {
                    hora     = Calendar.getInstance()
                    fecha    = Integer.valueOf(String.format("%d%02d%02d",f("YEAR"),f("MONTH"),f("DAY_OF_MONTH")))
                    h        = Integer.valueOf(String.format("%02d%02d",f("HOUR_OF_DAY"),f("MINUTE")))

                    registro = registros[i]==null?0:registros[i]
                    tarea    = tareas[i]
                    
                    if(h > tarea.hora && registro < getDia()) {
                        println "[ok->${tarea.tarea}, fecha->${fecha}]"
                        if(Eval.me(tarea.comando)) { registros[i] = fecha }
                    }
                    
                }
                sleep intervalo
            }
        }
    }
}